package com.ld.admin.controller.system.codegen;

import javax.validation.Valid;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ld.admin.controller.AdminBasicController;
import com.ld.shieldsb.codegen.model.PreferenceModel;
import com.ld.shieldsb.codegen.util.PreferenceUtil;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.web.util.Web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "代码生成器-个人偏好设置")
@ConditionalOnProperty(prefix = "modules.sc.system.codegen", name = "preference.enable", havingValue = "true", matchIfMissing = true)
@Controller
@RequestMapping("${shieldsb.adminPath}/system/codegen/preference")
public class CodegenPreferenceController extends AdminBasicController<Object> {

    @ApiOperation(value = "首页", notes = "")
    @RequestMapping(path = "", method = RequestMethod.GET)
    public String index() {
        request.setAttribute("model", PreferenceUtil.getPreferenceModel(Web.Util.getSessionUserId(request) + ""));
        return "a/system/codegen/preference/preference_show";
    }

    @ApiOperation(value = "添加/修改代码生成器-个人偏好设置", notes = "")
    @RequestMapping(path = "/addOrUpdate", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object addOrUpd(@Valid PreferenceModel model) {
        String userId = Web.Util.getSessionUserId(request) + "";
        PreferenceModel oldModel = PreferenceUtil.getPreferenceModel(userId);
        model.setRecentSrcDirs(oldModel.getRecentSrcDirs());
        Result result = PreferenceUtil.savePreferenceModel(model, userId);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "从最近操作记录中删除目录", notes = "")
    @RequestMapping(path = "/delRecentSrcDir", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object delRecentSrcDir() {
        int index = getInt("index"); // 从1开始
        Result result = PreferenceUtil.delRecentSrcDir(index, Web.Util.getSessionUserId(request) + "");
        return Web.Response.result(result);
    }

    @ApiOperation(value = "重置个人偏好信息", notes = "")
    @RequestMapping(path = "/reset", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object reset() {
        Result result = PreferenceUtil.reset(Web.Util.getSessionUserId(request) + "");
        return Web.Response.result(result);
    }

}